package org.xielei.designpatterns.structural.flyweight;

import java.util.HashMap;

/**
 * FlyweightFactory
 * 享元工厂
 *
 * @author xielei
 * @date 2020/8/6
 */
public class FlyweightFactory {

    private final HashMap<String, AbstractFlyweight> flyweight = new HashMap<>();

    /**
     * 获取具体享元
     * @param key
     * @return
     */
    public AbstractFlyweight getFlyweight(String key) {
        AbstractFlyweight abstractFlyweight = this.flyweight.get(key);

        if (abstractFlyweight == null) {
            abstractFlyweight = new ConcreteFlyweight(key);
            flyweight.put(key, abstractFlyweight);
        } else {
            System.out.println(key + " 已存在，被成功获取");
        }

        return abstractFlyweight;
    }
}
